﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Data.SqlClient;
using System.Threading;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class RegistrationMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.RegistrationMessage_PageTitle;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                OnlineUser ou = new OnlineUser();
                // The following code was replaced with RefreshOU function that will be used in a couple more places.
                //ou.UserId = GND.ReturnUserId();
                //ou.UserName = GND.ReadNivdalStringData("UserId", "Nivdal_GetUserNameById", ou.UserId);
                //ou.TeamId = GND.ReadNivdalIntData("@UserId", "Nivdal_GetTeamIdByUserId", ou.UserId);
                //ou.UserAccessLevel = GND.ReadNivdalIntData("@UserId", "Nivdal_GetUserLevel", ou.UserId);
                //ou.Language = GND.GetUserLanguage();
                //ou.IsPremium = GND.IsUserPremium();
                //ou.ApprovalStatus = (GND.ReadNivdalIntData("@UserId", "Nivdal_GetUserApprovalStatus", ou.UserId));
                //ou.Culture = GND.ReadNivdalStringData("@UserId", "Nivdal_GetUserCulture", ou.UserId);
                //ou.UICulture = GND.ReadNivdalStringData("@UserId", "Nivdal_GetUserUICulture", ou.UserId);
                //ou.TextDir = GND.ReadNivdalStringData("UserId", "Nivdal_GetUserTextDirection", ou.UserId);
                //ou.Country = GND.ReadNivdalIntData("UserId", "Nivdal_GetCountryByUser", ou.UserId);
                //ou.Newspaper = GND.ReadNivdalIntData("Country", "Nivdal_GetNewspaperByUser", ou.Country);
                //ou.NewsPaperPrivsLevel = GND.ReadNivdalIntData("UserId", "NewsPapaerId", "Nivdal_GetNewsPaperPrivsLevel", ou.UserId, ou.Newspaper);
                //ou.CanPlayFriendlies = GND.ReadNivdalBoolData("TeamId", "Nivdal_FriendlyCanTeamPlayFriendlies", ou.TeamId);
                //ou.IsQnAEditor = GND.ReadNivdalBoolData("UserId", "Nivdal_IsUserQnATipEditor", ou.UserId);

                ou.RefreshOU();

                Session["User"] = ou;

                TransferMarket tm = new TransferMarket
                {
                    InstinctsMin = "0",
                    InstinctsMax = "100",
                    GoalkeepingMin = "0",
                    GoalkeepingMax = "100",
                    PositioningMin = "0",
                    PositioningMax = "100",
                    StaminaMin = "0",
                    StaminaMax = "100",
                    PowerMin = "0",
                    PowerMax = "100",
                    TacklingMin = "0",
                    TacklingMax = "100",
                    HeadingMin = "0",
                    HeadingMax = "100",
                    QuicknessMin = "0",
                    QuicknessMax = "100",
                    TechniqueMin = "0",
                    TechniqueMax = "100",
                    ShortPassingMin = "0",
                    ShortPassingMax = "100",
                    LongPassingMin = "0",
                    LongPassingMax = "100",
                    ShootingMin = "0",
                    ShootingMax = "100",
                    AgeMin = "16",
                    AgeMax = "40",
                    CharismaMin = "0",
                    CharismaMax = "100",
                    ExperienceMin = "0",
                    ExperienceMax = "100",
                    Nationality = "10",
                    PlyerTyperadio = "3",
                    Deadline = "3",
                    CurrentBidMax="99999999",
                    CurrentBidMin="0",
                    PosCen="1",
                    PosDef="1",
                    PosFor="1",
                    PosGK="1",
                    PosLeft="1",
                    PosMid="1",
                    PosRight="1"                    
                };
                Session["SearchTransferMarketValues"] = tm;

                // init newbie tip
                int NewbieTipStage;
                if (int.TryParse(GND.ReadNivdalIntData("UserId", "Nivdal_NewbieTipsGetUserTipStage", ou.UserId), out NewbieTipStage))
                    Session["NewbieTipStage"] = NewbieTipStage;
                if (NewbieTipStage < 100)
                {
                    Session["NewbieTipText"] = GND.ReadNivdalStringData("TipId", "Language", "dbo.Nivdal_NewbieTipsGetTip", NewbieTipStage, ou.Language);
                }

                if (ou.ApprovalStatus == "3")
                {
                    Response.Redirect("~/RegistrationDeclineMessage.aspx");
                }
                if (ou.ApprovalStatus == "5")
                {
                    Response.Redirect("~/TeamHomePage.aspx");
                }
                if ((ou.ApprovalStatus == "1") || (ou.ApprovalStatus == "4"))
                {
                    Response.Redirect("~/GameRules.aspx");
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            else
            {
                Session["User"] = new Nivdal.AuxCode.OnlineUser
                {
                    UICulture = "en",
                    Culture = "en-US",
                    TextDir = "LeftToRight",
                    Language = "2",
                    Newspaper = null,
                    NewsPaperPrivsLevel = "0",
                    ApprovalStatus = "0",
                    Country = null,
                    IsPremium = false,
                    TeamId = null,
                    UserAccessLevel = "0",
                    UserId = null,
                    UserName = "guest"
                };
                Response.Redirect("~/Default.aspx");
            }

        }

        protected override void InitializeCulture()
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string UserUICulture = GND.GetUserUICulture();
            string UserCulture = GND.GetUserCulture();
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserUICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
        }
    }
}